import pymysql

from pyecharts import options
from pyecharts.charts import Bar, Pie

#创建数据库连接
db = pymysql.connect(
     host='localhost',
     port=3306,
    database='crawlerdemo',
     user='root',
     passwd='123456',
     charset='utf8'
 )
curses = db.cursor();
sql  = "select avg(job_sal),job_place from job group by job_place order by avg(job_sal) desc limit 5"

sal = []
city = []
try:
    curses.execute(sql)
    res = curses.fetchall()

    for row in res:
        sal.append(row[0])
        city.append(row[1])
except Exception as e:
    print("查询出了问题")

#创建柱状图对象
bar = Bar()
# 添加X轴的数据
bar.add_xaxis(city)
#添加y轴的数据
bar.add_yaxis("月薪/元",sal)
#添加图标标题
bar.set_global_opts(title_opts=options.TitleOpts(title="平均工资最低的五个城市",subtitle="单位/元"))
bar.render("baravg.html")